package com.lz.football.module.shop
{
	import com.common.stdlib.ArrayUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.events.ShopEvent;
	import com.liangzhong.message.http.JsClient;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.proxy.Shop;
	
	import common.BaseView.BaseView;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.nick.PagePanel;

	/**
	 *商城 
	 * @author Administrator
	 * 
	 */	
	public class ShopView extends BaseView implements IShop
	{
		/**
		 * 
		 */
		private var listContain:MovieClip;
		/**
		 * 
		 */
		private var itemVec:Vector.<ShopItem>;
		/**
		 *购买面板 
		 */
		private var _buyView:ShopBuyView;
		/**
		 *换页 
		 */
		private var _page:PagePanel;
		private var topupBtn:SimpleButton;
		public function ShopView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.modul.shop.shopView");
			this.addChild(symbolSkin);
			_windowBg = symbolSkin.shopBg;
			isDrag = true;
			isCloseBtn = true;
			moveClose(617,25);
			_viewHeight = symbolSkin.height;
			_viewWidth = symbolSkin.width;
			topupBtn = symbolSkin.topupBtn
			onResizeHandler();
			listContain = symbolSkin.listContain;
			initItem();
			initPage();
			shopInfo();
			
			addEvtListener();
		}
		private function initPage():void
		{
			_page = new PagePanel();
			_page.initUI();
			this.addChild(_page);
			_page.x = 290;
			_page.y = 440;
			_page.callBack = trunPage;
			
		}
		override public function addEvtListener():void
		{
			AppData.getInstance().shopDataManager.addEventListener(ShopEvent.SHOP_GOODSLIST,onShopListHandler);
			topupBtn.addEventListener(MouseEvent.CLICK,ontopUphandler);
		}
		
		protected function ontopUphandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			JsClient.recharge();
		}
		
		protected function onShopListHandler(event:ShopEvent):void
		{
			// TODO Auto-generated method stub
			shopInfo();
		}
		/**
		 *初始化 
		 * 
		 */
		private function initItem():void
		{
			if(itemVec != null)return;
			var item:ShopItem;
			itemVec = new Vector.<ShopItem>();
			for(var i:int = 0;i < 9;i++)
			{
				item = new ShopItem();
				item.initUI();
				item.x = (item.width + 20) * (i% 3);
				item.y = (item.height + 38) * int (i / 3);
				itemVec.push(item);
				item.callBack = selectItem;
				listContain.addChild(item);
			}
//			shopInfo();
		}
		/**
		 *显示列表数据 
		 * 
		 */		
		private function shopInfo():void
		{
			initItem();
			while(listContain.numChildren)
			{
				listContain.removeChildAt(0);
			}
			var arr:Array = AppData.getInstance().shopDataManager.shopList;
			if(arr == null)return;
			_page.max = Math.ceil(arr.length / 9);
			if(_page.max <_page.min)_page.max = 1;
		
			var len:int = arr.length > itemVec.length?itemVec.length:arr.length;
			for(var i:int = 0;i < len;i++)
			{
				
				itemVec[i].setData(arr[i]);
				listContain.addChild(itemVec[i]);
			}
		}
		
		/**
		 *换页 
		 * @param $type
		 * 
		 */
		private function trunPage($type:String = ""):void
		{
			while(listContain.numChildren)
			{
				listContain.removeChildAt(0);
			}
			
			var arr:Array = ArrayUtil.getPageSizeDatas(_page.min,9,AppData.getInstance().shopDataManager.shopList);
			var len:int = arr.length > itemVec.length?itemVec.length:arr.length;
			for(var i:int = 0;i < len;i++)
			{
				
				itemVec[i].setData(arr[i]);
				listContain.addChild(itemVec[i]);
			}
		}
		/**
		 *选择了item 
		 * @param $vo
		 * 
		 */
		private function selectItem($vo:GoodsVo):void
		{
			if($vo == null)return;
			if(_buyView == null)
			{
				_buyView = new ShopBuyView();
				_buyView.initUI();
			}
			_buyView.setData($vo);
			GlobalDef.GameWindowContainer.addChild(_buyView);
		}
		override public function onCloseHandler(event:MouseEvent):void
		{
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE));
		}
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
			AppData.getInstance().shopDataManager.removeEventListener(ShopEvent.SHOP_GOODSLIST,onShopListHandler);
			topupBtn.removeEventListener(MouseEvent.CLICK,ontopUphandler);
		}
		override public function destroyView():void
		{
			removeEvtListener();
			if(_buyView != null)
				_buyView.destroyView();
			_buyView  = null;
			_page.destroyView();
			_page = null;
//			var item:ShopItem;
			while(itemVec.length)
			{
				itemVec.pop().destroyView();
			}
			itemVec = null;
			super.destroyView();
		}
	}
}